package com.gui.juc.base.thread.status;

import com.gui.utils.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadStatus {

    private static final Object lockObject = new Object();

    public static void main(String[] args) {
        // testBlocked();
        testTimeWaiting();
    }

    private static void testBlocked() {

        Thread t1 = new Thread(() -> {
            synchronized (lockObject) {
                Sleeper.sleep(TimeUnit.SECONDS, 5);
                log.debug("t1执行结束...");
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (lockObject) {
                Sleeper.sleep(TimeUnit.SECONDS, 5);
                log.debug("t2执行结束...");
            }
        });
        log.debug("开始执行....");
        t1.start();
        Sleeper.sleep(TimeUnit.SECONDS, 1);
        t2.start();
        Sleeper.sleep(TimeUnit.SECONDS, 1);
        log.debug("t2的状态为：{}", t2.getState());
    }

    private static void testTimeWaiting() {

        Thread thread = new Thread(() -> {
            Sleeper.sleep(TimeUnit.SECONDS, 5);
            log.debug("thread执行完成");
        });
        thread.start();

        Sleeper.sleep(TimeUnit.SECONDS, 1);
        log.debug("thread状态：{}", thread.getState());
    }
}
